package com.zaofan.mobile.controller;

import com.zaofan.bean.Account;
import com.zaofan.bean.Comment;
import com.zaofan.bean.OrderInfo;
import com.zaofan.exception.ServiceException;
import com.zaofan.mobile.bean.PayBean;
import com.zaofan.mobile.bean.UserOrder;
import com.zaofan.service.AccountService;
import com.zaofan.service.ComplaintsService;
import com.zaofan.service.OrderInfoService;
import com.zaofan.service.remote.RemoteService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/mobile/user/order")
public class OrderController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;
	private AccountService accountService = new AccountService();
	private OrderInfoService orderInfoService = new OrderInfoService();
	private ComplaintsService complaintsService = new ComplaintsService();



	/**
	 * 用户 提交订单接口 周套餐接口
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void packages(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.getSession().removeAttribute("CouponUserId");
		request.getSession().removeAttribute("CouponId");
		request.getSession().removeAttribute("CouponName");

		try {

			String platform = "";
			platform = request.getParameter("platform");
			if (null==platform){
				platform="";
			}


			String orderInfo = request.getParameter("orderInfo");
			if (orderInfo == null) {
				JsonUtils.writeBeanFailJson("请填写正确的订单信息后在提交", response);
				return;
			}


			System.out.println("平台为-----------"+platform);
			PayBean bean = orderInfoService.submitPackageOrder(orderInfo,platform);
            request.getSession().removeAttribute("shopCar");
            request.getSession().removeAttribute("totalSC");
            JsonUtils.writeSuccessJson("订单提交成功", bean, response);
			return;
		} catch (ServiceException service) {
			JsonUtils.writeFailJson(service.getMessage(), response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("订单提交失败", response);
		}
		JsonUtils.writeBeanFailJson("订单提交失败!!", response);
	}

	/**
	 * 用户 订单列表接口
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			String userId = request.getParameter("userId");
			Integer accountId = Integer.valueOf(userId);
			Integer begin = 0;
			Integer end = 10;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			Account account = accountService.findById(accountId);
			if (account == null) {
				JsonUtils.writeListFailJson("账号信息错误!!", response);
				return;
			}
			List<UserOrder> order = orderInfoService.findByAcccountId(accountId, begin, end);
			JsonUtils.writeSuccessJson("查询成功", order, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}
	/**
	 * 用户 订单列表接口
	 *
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			String userId = request.getParameter("userId");
			Integer accountId = Integer.valueOf(userId);
			Integer begin = 0;
			Integer end = 10;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			Account account = accountService.findById(accountId);
			if (account == null) {
				JsonUtils.writeListFailJson("账号信息错误!!", response);
				return;
			}
			List<UserOrder> order = orderInfoService.findByAcccountId2(accountId, begin, end);
			JsonUtils.writeSuccessJson("查询成功", order, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	/**
	 * 用户 订单删除接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String id = request.getParameter("id");
			Integer orderId = Integer.valueOf(id);
			orderInfoService.deleteByOrderId(orderId);
			JsonUtils.writeSuccessJson("删除成功", "", response);
			return;
		} catch (ServiceException service) {
			JsonUtils.writeFailJson(service.getMessage(), response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("删除失败", response);
		}
		JsonUtils.writeBeanFailJson("删除失败!!", response);
	}

	/**
	 * 用户 订单详细接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id = request.getParameter("id");
			Integer orderId = Integer.valueOf(id);
			UserOrder order = orderInfoService.getOrderInfo(orderId);
			JsonUtils.writeSuccessJson("查询成功", order, response);
			return;
		} catch (ServiceException service) {
			JsonUtils.writeFailJson(service.getMessage(), response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!", response);
	}

	/**
	 * 用户 订单评论接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void comment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Comment comment = WebUtils.write2bean(request, Comment.class);
			orderInfoService.addOrderComment(comment);
			JsonUtils.writeSuccessJson("我们会继续努力为您提供更好的服务!!", "", response);
			return;
		} catch (ServiceException service) {
			JsonUtils.writeFailJson(service.getMessage(), response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("评论失败", response);
		}
		JsonUtils.writeFailJson("评论失败!!", response);
	}

	/**
	 * 用户 余额支付接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void pay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountService accountService = new AccountService();
		RemoteService remoteService = new RemoteService();
		try {
			String orderNo = request.getParameter("orderNo");
			orderInfoService.payOrderByAccountMoney(orderNo);
			OrderInfo orderInfo = orderInfoService.findParentByOrderNo(orderNo);
			if (orderInfo.getIsCoupon()==1){
				remoteService.updateCouponStatus(orderNo);
			}
            com.zaofan.mobile.bean.Account account = (com.zaofan.mobile.bean.Account) request.getSession().getAttribute("userinfo");
            if (account != null){
                request.getSession().setAttribute("userinfo",accountService.findAccountInfo(account.getId()));
            }
			JsonUtils.writeSuccessJson("支付成功!!", "", response);
			return;
		} catch (ServiceException service) {
			JsonUtils.writeFailJson(service.getMessage(), response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("支付失败", response);
		}
		JsonUtils.writeFailJson("支付失败!!", response);
	}

	/**
	 * 用户 订单投诉
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void complaints(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Integer orderId = Integer.valueOf(request.getParameter("orderId"));
			String content = request.getParameter("content");
			complaintsService.complaints(orderId, content);
			JsonUtils.writeSuccessJson("感谢您的反馈,我们会努力为您解决的!!", "", response);
			return;
		} catch (ServiceException service) {
			JsonUtils.writeFailJson(service.getMessage(), response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("反馈失败", response);
		}
		JsonUtils.writeFailJson("反馈失败!!", response);
	}

}
